package dao;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import entity.Question;
import entity.User;

//TODO day02 文档管理员
public class EntityContext {
	private Config config;
	/**存放所有的User类**/
	private Map<Integer,User> users=new HashMap<Integer, User>();
	/**存放所有的问题*/
	public List<Question>  questions=new ArrayList<Question>();
	/**按照等级分类的Question集合*/
	public Map<Integer, List<Question>> levels=new HashMap<Integer, List<Question>>();
	/**存放考试规则*/
	private List<String> rules=new ArrayList<String>();
	public EntityContext(Config config) throws IOException {
		this.config=config;
		loadUsers();//将user.txt中的内容装载进来
		loadQuestions();
		loeadrules();
	}
	public List<String> getRules() {
		return rules;
	}
	private void loeadrules() throws IOException {
		String filename=config.getString("rule");
		BufferedReader in=new BufferedReader(new FileReader(filename));
		String line;
		while((line=in.readLine())!=null){
			if (line.trim().equals("")) {
				continue;
			}
			rules.add(line);
		}
	}
	private void loadQuestions() throws IOException {
		String filename=config.getString("QuestionFile");
		BufferedReader in=new BufferedReader(new FileReader(filename));
		String line;
		while((line=in.readLine())!=null){
			if (line.trim().equals("")) {
				continue;
			}
			//@answer=2/3,score=5,level=5
			if (line.matches("^@answer=\\d+(/\\d+)?,score=\\d,level=\\d\\d?$")) {
				System.out.println("line:"+line);
				Question q=parseField(line);
				readTitle(q,in);
				readOption(q,in);
				questions.add(q);
				addBylevel(q);
			}
		}
	}
	private void addBylevel(Question q) {
		List<Question> list=levels.get(q.getLevel());
		if (list==null) {
			list=new ArrayList<Question>();
			levels.put(q.getLevel(), list);
		}
		list.add(q);
	}
	private void readOption(Question q, BufferedReader in) throws IOException {
		List<String> options=new ArrayList<String>();
		String line=in.readLine();
		while(!line.startsWith("$")){
			options.add(line);
			line=in.readLine();
		}
		q.setOptionNums(options.size());
		q.setOptions(options);
	}
	private void readTitle(Question q, BufferedReader in) throws IOException {
		q.setTitle(in.readLine());
	}
	//解析题目的第一行数据
	//@answer=2/3,score=5,level=5
	private Question parseField(String line) {
		String [] data=line.split("[@,]");
		Question q=new Question();
		q.setId(questions.size()+1);
		q.setAnswer(parseAnswer(data[1].split("=")[1]));
		q.setScore(Integer.parseInt(data[2].split("=")[1]));
		q.setLevel(Integer.parseInt(data[3].split("=")[1]));
		return q;
	}
	private List<Integer> parseAnswer(String line) {
		List<Integer> answers=new ArrayList<Integer>();
		String [] data=line.split("/");
		for(String s:data){
			answers.add(new Integer(s));
		}
		return answers;
	}
	private void loadUsers() throws IOException {
		String filename=config.getString("UserFile");
		BufferedReader in=new BufferedReader(new FileReader(filename));
		String line;
		while((line=in.readLine())!=null){
			if (line.trim().equals("")) {
				continue;
			}
			User u=parseUser(line);
			users.put(u.getId(), u);
		}
	}
	//TODO day02 将一行文本解析成User对象
	private User parseUser(String line) {
		//1001:430724198001122345:刘苍松:81dc9bdb52d04dc20036dbd8313ed055:13810381038:liucs@tarena.com.cn
		User u=new User();
		String data[]=line.split(":");
		u.setId(Integer.parseInt(data[0]));
		u.setCertifyNumber(data[1]);
		u.setName(data[2]);
		u.setPassword(data[3]);
		u.setPhone(data[4]);
		u.setEmail(data[5]);
		return u;
	}
	//根据题号获取题目
	public Question getQuestion(int id){
		for (Question q:questions) {
			if (q.getId()==id) {
				return q;
			}
		}
		return null; 
	}
	public int getTimeLimit(){
		return config.getInt("TimeLimit");
	}
	public String getPaperTitle(){
		return config.getString("PaperTitle");
	}
	public User getUser(int id){
		return users.get(id);
	}
	public List<Question> getQuestions(int level){
		return new ArrayList<Question>(levels.get(level));
	}
	
	public Config getConfig() {
		return config;
	}
	public static void main(String[] args) throws IOException {
		Config config=new Config("client.properties");
		EntityContext c=new EntityContext(config);
		//System.out.println(c.getUser(1001));
	}
	
}
